package com.yunxi.controller.sat;

import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.order.pojo.OrderCustomerVO;
import com.yunxi.order.service.OrderCustomerDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 零售订单 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-07-10
 */
@RestController
@RequestMapping("/admin/satOrderCustomer")
@Api(tags = "sat:orderCustomer", description = "订单接口")
public class SatOrderCustomerController {
    private static final Logger logger = LoggerFactory.getLogger(SatOrderCustomerController.class);

    @Autowired
    OrderCustomerDubboService orderCustomerDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询零售订单分页")
    public ApiResult page(OrderCustomerVO orderCustomerVO, Integer pageNum, Integer pageSize, @CurrentUser LoginInfo loginInfo) throws Exception {
        orderCustomerVO.setCurSys("sat");
        return ResponseUtil.ok(orderCustomerDubboService.findPageByVO(orderCustomerVO, pageNum, pageSize,loginInfo));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询零售订单列表")
    public ApiResult list(OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(orderCustomerDubboService.listByVo(v,loginInfo));
    }

    @GetMapping("/listAllByCustomerNo")
    @ApiOperation(value = "查询该客户零售订单列表")
    public ApiResult listAllByCustomerNo(OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(orderCustomerDubboService.listAllByCustomerNo(v,loginInfo));
    }

    @GetMapping("/loadDetail")
    @ApiOperation(value = "读取零售订单详情")
    public ApiResult loadDetail(@RequestParam String orderNo) {
        return ResponseUtil.ok(orderCustomerDubboService.load(orderNo));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存零售订单信息")
    public ApiResult save(@RequestBody OrderCustomerVO orderCustomerVO, @CurrentUser LoginInfo loginInfo) throws Exception {

        if(loginInfo.getPartner()!=null&& StringUtils.isEmpty(orderCustomerVO.getPartnerCode())){
            Object p = loginInfo.getPartner();
            PartnerVO partnerVO = JSONObject.parseObject(JSONObject.toJSONString(p),PartnerVO.class);
            orderCustomerVO.setPartnerCode(partnerVO.getPartnerCode());
        }
        if(StringUtils.isEmpty(orderCustomerVO.getOrderCustomerId())){
//            orderCustomerVO.setOrderSource(2);
            return orderCustomerDubboService.save(orderCustomerVO,loginInfo);
        }else{
            return orderCustomerDubboService.update(orderCustomerVO,loginInfo);
        }
    }

   @GetMapping("/allList")
   @ApiOperation(value = "销售顾问本月全部订单")
   public ApiResult allList(OrderCustomerVO orderCustomerVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(orderCustomerDubboService.allList(orderCustomerVO, loginInfo));
   }


    @PostMapping("/cancelAudit")
    @ApiOperation(value = "订单退订审核")
    public ApiResult cancelApply(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
        return orderCustomerDubboService.cancelApplyPC(v, loginInfo);
    }

    @PostMapping("/updateDeliveryDate")
    @ApiOperation(value = "修改预计交车时间,交车人")
    public ApiResult updateDeliveryDate(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
       if (StringUtils.isEmpty(v.getOrderCustomerId())) return ResponseUtil.fail(-1,"orderCustomerId 订单ID不能为空");
       if (StringUtils.isEmpty(v.getOrderCustomerNo())) return ResponseUtil.fail(-1,"orderCustomerNo 零售单编号不能为空");
        orderCustomerDubboService.updateDeliveryInfo(v, loginInfo);
        return ResponseUtil.ok();
    }
}
